<template>
  <div class="app-container">
    <el-card class="query-card">
      <el-form ref="query" :model="page.query" inline label-position="right" label-width="80px">
        <el-form-item label="收款日期" prop="receiptDateRange">
          <el-date-picker
            v-model="page.query.receiptDateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收款编号" prop="receiptNo">
          <el-input v-model="page.query.receiptNo" placeholder="请输入收款编号" clearable />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="page.query.orderNo" placeholder="请输入订单编号" clearable />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerId">
          <customer-query ref="customerQuery" v-model="page.query.customerId" @choose="handleChooseCustomer" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQueryForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="table-card">
      <el-tabs v-model="page.query.receiptMethod" @tab-click="handleTabClick">
        <el-tab-pane
          v-for="(item,index) in receiptMethodList"
          :key="index"
          :label="item.stateName+'('+item.count+')'"
          :name="item.state"
        />
      </el-tabs>
<!--      <div>
        <span>分类属性列表</span>
      </div>
      <el-divider />-->
      <div class="table-button">
        <el-button type="primary" class="el-icon-plus" @click="saveClick">新增
        </el-button>
        <el-button type="danger" plain class="el-icon-folder-delete" @click="handleBatchDelete">批量删除</el-button>
        <el-tooltip content="刷新" class="table-right-btn">
          <el-button size="mini" circle icon="el-icon-refresh" @click="doPage" />
        </el-tooltip>
      </div>
      <el-table
        v-loading="tableLoading"
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="45" />
        <el-table-column label="收款编号" prop="receiptNo" />
        <el-table-column label="收款日期" prop="receiptDate" min-width="100" />
        <el-table-column label="订单编号" prop="orderNo" min-width="110">
          <template slot-scope="scope">
            <el-button type="text" @click="toOrderDetail(scope.row)">{{ scope.row.orderNo }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="客户名称" prop="customerName" />
        <el-table-column label="金额" prop="amount">
          <template slot-scope="scope">
            <money-format v-model="scope.row.amount" />
          </template>
        </el-table-column>
        <el-table-column label="收款方式" prop="receiptMethod">
          <template slot-scope="scope">
            <dict-table-column v-model="scope.row.receiptMethod" dict-key="payment_method" />
          </template>
        </el-table-column>
        <el-table-column label="备注" prop="remark" show-overflow-tooltip />
        <el-table-column label="操作" min-width="100">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="updateClick(scope.row)"
            >修改
            </el-button>
            <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='取消'
              placement="top"
              style="margin-left: 10px"
              title="是否删除此条数据"
              @confirm="handleDelConfirm(scope.row)"
            >
              <el-button slot="reference" type="text">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-sizes="[10, 20, 30, 40]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <div v-if="dialogVisible">
      <submit-module ref="submitModule" :visible="dialogVisible" @close="handleClose" />
    </div>
    <div v-if="batchSaveVisible">
      <batch-save-module ref="batchSaveModule" :visible="batchSaveVisible" @close="handleClose" />
    </div>
  </div>
</template>

<script>
import { deleteReceipt, selectReceiptPage, selectReceiptMethodCount } from '@/api/financial/receipt'
import SubmitModule from '@/views/financial/receipt/modules/submitModule.vue'
import BatchSaveModule from '@/views/financial/receipt/modules/batchSaveModule.vue'

export default {
  name: 'Index',
  components: { BatchSaveModule, SubmitModule },
  data() {
    return {
      tableData: [],
      tableLoading: false,
      selected: [],
      receiptMethodList: [],
      page: {
        page: 1,
        size: 10,
        total: 0,
        query: {
          receiptNo: '',
          receiptDateRange: [],
          customerId: '',
          receiptMethod: '-1',
          orderNo: ''
        }
      },
      dialogVisible: false,
      batchSaveVisible: false
    }
  },
  mounted() {
    this.doPage()
  },
  methods: {
    handleSearch() {
      this.page.page = 1
      this.doPage()
    },
    doPage() {
      // 分页查询
      this.tableLoading = true
      selectReceiptPage(this.page).then(resp => {
        if (resp.success) {
          this.tableData = resp.data.records
          this.page.total = resp.data.total
        }
      }).finally(() => {
        this.tableLoading = false
      })
      this.selectReceiptMethodCount()
    },
    // 选中事件
    handleSelectionChange(row) {
      this.selected = row
    },
    resetQueryForm() {
      this.$refs.query.resetFields()
      this.$refs.customerQuery.resetQueryForm()
      this.page.query.receiptMethod = '-1'
      this.doPage()
    },
    // 新增按钮点击
    saveClick() {
      this.batchSaveVisible = true
      this.$nextTick(() => {
        this.$refs.batchSaveModule.init()
      })
    },
    // 修改按钮点击
    updateClick(row) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.submitModule.init(row.id)
      })
    },
    // 批量删除点击
    handleBatchDelete() {
      if (this.selected.length === 0) {
        this.$message.warning('请选择一条或多条数据')
        return
      }
      this.$confirm('是否批量删除选中的数据?', '删除提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        this.tableLoading = true
        const data = { ids: this.selected.map(item => item.id) }
        deleteReceipt(data).then(resp => {
          if (resp.success) {
            this.$message.success('删除成功')
          }
        }).finally(() => {
          this.tableLoading = false
          this.doPage()
        })
      })
    },
    // 分页大小
    handleSizeChange(val) {
      this.page.size = val
      this.doPage()
    },
    // 页码
    handleCurrentChange(val) {
      this.page.page = val
      this.doPage()
    },
    // 表单关闭
    handleClose() {
      this.batchSaveVisible = false
      this.dialogVisible = false
      this.doPage()
    },
    handleDelConfirm(row) {
      this.tableLoading = true
      const data = { ids: [row.id] }
      deleteReceipt(data).then(resp => {
        if (resp.success) {
          this.$message.success('删除成功')
          this.doPage()
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    handleChooseCustomer(row) {
      if (row) {
        this.page.query.customerId = row.id
      }
    },
    // 详情按钮点击
    toOrderDetail(row) {
      this.$router.push({ path: '/order/saleOrder/detail', query: { id: row.orderId, from: this.$route.path }})
    },
    selectReceiptMethodCount() {
      this.tableLoading = true
      selectReceiptMethodCount(this.page.query).then(resp => {
        if (resp.success) {
          this.receiptMethodList = resp.data
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    handleTabClick(tab) {
      this.page.query.receiptMethod = tab.name
      this.doPage()
    }
  }
}
</script>

<style scoped>

</style>
